/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.mvc.views.platformer
{
	import com.puny.mvc.views.singleviews.RichView;
	import com.puny.utils.Random;
	
	import flash.display.FrameLabel;

	public class EnemyView extends RichView
	{
		private var _damagedLevel:String ="";
		private var _attacks:Array = new Array();
		public function EnemyView(src:Object, exportedName:String)
		{
			super(src, exportedName);
		}
		
//		override public function get height():Number {
//			return _rect[_mc.currentLabel].height;
//		}

//		override public function get width():Number {
//			trace(_rect[_mc.currentLabel]);
//			return _rect[_mc.currentLabel].width;
//		}
		private function adjustPosition(label:String):void {
			if (_mc !=null) {
				//For each frame label
//				_mc.x = - maxRect.x;
//				_mc.y = - maxRect.y;
//				graphics.clear();
//				graphics.beginFill(0xFFFFFF,0);
//				graphics.drawRect(0,0,maxRect.width,maxRect.height);
//				graphics.endFill();
				_mc.x = -maxRect.x;
				_mc.y = -maxRect.y;
				graphics.clear();
				graphics.beginFill(0xFFFFFF,0);
				graphics.drawRect(0,0,maxRect.width,maxRect.height);
				graphics.endFill();
			}
		}
		
		public function set damagedLavel(value:String):void {
			_damagedLevel = value;
		}
		
		override public function afterOpen():void {
			super.afterOpen();
			var attack:String;
			if (movieClip != null) {
				for each(var label:FrameLabel in movieClip.currentLabels) {
					attack = label.name.substr(0,6);
					if (attack == "attack" && label.name.length < 9) {
						trace(label.name);
						_attacks.push(label.name);
					}
				}
			}
		}
		
		override public function gotoAndPlay(label:String):Boolean {
			var ret:Boolean = super.gotoAndPlay(label)
			adjustPosition(label);
			return ret;
		}
		
 		public function attack():String {
 			var index:int = Random.integer(_attacks.length);
			var label:String = _attacks[index];
			gotoAndPlay(label+_damagedLevel);
			return label+_damagedLevel;
		}
		
		override public function gotoAndStop(label:String):Boolean {
			var ret:Boolean = super.gotoAndStop(label); 
			adjustPosition(label);
			return ret;
		}
		
		public function get currentLabel():String {
			return _mc.currentLabel;
		}
		override public function procedureMainFrame():Boolean {
//			trace(_mc.currentFrame,_mc.currentLabel)
			if ( isFinalOf("walking")) {
				gotoAndPlay("walking");
			} else if ( isFinalOf(_attacks[0]+_damagedLevel) || isFinalOf("hit")) {
				gotoAndPlay("walking");
			}
			return true;
		}
		
	}
}